﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Microsoft.Xrm.Sdk;
using Moq;
using XRM.Framework.Core;
using XRM.Solutions.FrameworkSample.Common;
using XRM.Solutions.FrameworkSample.Plugins;
using XRM.Framework.Test.Unit;

namespace XRM.Solution.FrameworkSample.UnitTests.Plugins
{
    [TestClass]
    public class TestPreAccountCreateNoAccountNumber : PluginUnitTest
    {
        #region Instance Variables

        private string autoNumber;
        Mock<IAutoNumberGenerator> generatorMock;

        #endregion

        #region Do

        [TestMethod]
        public void TestGenerateAccountNumber()
        {
            base.Test();
        }

        #endregion

        #region Abstract Method Implementation

        protected override IPlugin Setup()
        {
            base.SetPluginEvent("account", "Create",
                SdkMessageProcessingStepImage.PreOperation);

            Entity account = new Entity("account");
            base.SetTarget(account);

            autoNumber = "10000000";
            generatorMock = new Mock<IAutoNumberGenerator>();
            generatorMock.Setup(generator =>
                                generator.Generate("account", "accountnumber"))
                                .Returns(autoNumber);

            XrmContainer.Instance.Register<IAutoNumberGenerator>(generatorMock.Object);

            return new PreAccountCreate();
        }

        protected override void Verify()
        {
            Assert.IsNull(Error);

            Entity account = (Entity)GetTarget();

            Assert.IsTrue(account.Contains("accountnumber"));

            Assert.AreEqual(account["accountnumber"].ToString(), autoNumber);

            generatorMock.Verify(generator =>
                                generator.Generate("account", "accountnumber")
                                , Times.Once());
        }

        #endregion
    }
}
